//leetcode第二天
// 第1题
package leecode;

import java.util.HashMap;

class ListNode1 {
      int val;
      ListNode next;
      ListNode1(int x) { val = x; }
  }
 class Node1 {
    int val;
    Node1 next;
    Node1 random;

    public Node1(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class leetcode3 {

    public int[] reversePrint(ListNode head) {
        int count = 0;
        ListNode cur = head;
        while (head != null) {
            head = head.next;
            count++;
        }
        int[] arr = new int[count];
        for (int i = count - 1; i >= 0; i--) {
            arr[i] = cur.val;
            cur = cur.next;
        }
        return arr;
    }
    //第二题
    public ListNode reverseList(ListNode head) {
        ListNode pre=null;
        ListNode end=null;
        while (head!=null){
            pre=head.next;
            head.next=end;
            end=head;
            head=pre;
        }
        return end;
    }
    //第三题
    //利用哈希表进行存新旧对应元素，复制复杂链表
    public Node1 copyRandomList(Node1 head) {
        HashMap<Node1, Node1> hashMap = new HashMap<>();
        Node1 head1=head;
        Node1 head2=head;
        while (head != null) {
            Node1 cur= new Node1(head.val);
            hashMap.put(head, cur);
            head=head.next;
        }
        for (int i = 0; i < hashMap.size(); i++) {
            hashMap.get(head1).next=hashMap.get(head1.next);
            hashMap.get(head1).random=hashMap.get(head1.random);
            head1=head1.next;
        }
        return hashMap.get(head2);
    }
//热题1/100
    private int[] twoSum(int[] nums, int target) {
        HashMap <Integer,Integer>hashMap=new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if(hashMap.containsKey(nums[i])){
                return new int[]{i,hashMap.get(nums[i])};
            }
            hashMap.put(target-nums[i],i);
        }
        return new int[0];
    }
//热题2/100

}

